<template>
    <div class="pd20">
        <el-form :inline="true" :model="quertInfo" class="demo-form-inline">
            <el-form-item label="类型名称">
                <el-input v-model="quertInfo.name" placeholder="请输入课程类型名称" clearable />
            </el-form-item>
            
            <el-form-item>
                <el-button type="primary" @click="getcoursetypeList">搜索</el-button>
            </el-form-item>
        </el-form>
        <div class="mb20">
            <el-button @click="toAdd" type="primary" icon="Plus" plain>新增</el-button>
            <el-button @click="toScopeDelete" type="danger" icon="Delete" plain>删除</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%" row-key="id" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column fixed prop="id" label="id" width="150" />
            <el-table-column prop="name" label="类型名称" width="120" />
            
            <el-table-column fixed="right" label="操作" min-width="120">
                <template #default="scope">
                    <el-button link type="primary" size="small" @click="toEdit(scope.row)">修改</el-button>
                    <el-button link type="primary" @click="onDel(scope.row.id)" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="quertInfo.pageNum" v-model:page-size="quertInfo.pageSize"
            :page-sizes="[1, 5, 10, 20]" :size="size" :disabled="disabled" :background="false"
            layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
    </div>

    <!-- 添加，编辑弹框 -->
    <el-dialog v-model="saveOrUpdateVisible" :title="saveOrUpdateTitle" width="500">
        <el-form :model="coursetype">
            <el-form-item label="编号" :label-width="formLabelWidth">
                <el-input v-model="coursetype.id" autocomplete="off" />
            </el-form-item>

            <el-form-item label="分类名称" :label-width="formLabelWidth">
                <el-input v-model="coursetype.name" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="saveOrUpdate(row)">
                    {{ saveOrUpdateTitle }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'

//数据开始
const selectedIds=ref([]);

const coursetype = ref({
    id: 0,
    name: ''
})

const saveOrUpdateVisible = ref(false);
const saveOrUpdateTitle = ref('添加');
const quertInfo = ref({
   pageNum: 1,
   pageSize: 5,
   name: '' 
})

const total = ref(0);
//数据结束

const toScopeDelete = () => {
    if (selectedIds.value.length == 0) {
        ElMessage({
            message: '请选择要删除的记录！',
            type: 'error',
        })
        return;
    }else{
        ElMessageBox.confirm(
            '真的要删除吗？',
            '提示',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
           .then(() => {
              //调用后端删除接口
            axios.delete('/coursetypeRMB/deleteByIds',
                {
                    data: selectedIds.value
                }
            )
                .then(res => {
                    let result = res.data; 
                    if (result.code == 0) {
                        ElMessage({
                            message: '删除成功！',
                            type: 'success',
                        })
                        getcoursetypeList();
                        
                    } else {
                        ElMessage({
                            message: '删除失败！',
                            type: 'error',
                        })
                        return;
                    }
                })
           })
    }
}


const handleSelectionChange = (val) => {
    selectedIds.value = val.map(item => item.id);
    // console.log('数据：', selectedIds);
    // console.log('selectedIds', selectedIds.value);

}

const handleSizeChange = (val) => {
    //   console.log(`${val} items per page`)
    quertInfo.value.pageSize = val;
    quertInfo.value.pageNum = 1;
    getcoursetypeList();
}
const handleCurrentChange = (val) => {
    quertInfo.value.pageNum = val;
    //   console.log(`current page: ${val}`)
    getcoursetypeList();
}


const onDel = id => {
    // console.log('id', id);
    ElMessageBox.confirm(
        '真的要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            console.log('确定');
            //调用后端删除接口
            axios.delete('/coursetypeRMB/' + id)
                .then(res => {
                    // console.log('res', res);
                    // console.log('=========');
                    let result = res.data;  // resp是promise对象，.data返回我们自己的标准R 对象
                    if (result.code != 1) {
                        ElMessage({
                            message: '删除失败！',
                            type: 'error',
                        })
                        return;
                    } else {
                        ElMessage({
                            message: '删除成功！',
                            type: 'success',
                        })
                        getcoursetypeList();
                    }
                })

        })
}

//  去添加的页面
const toAdd = row => {
    saveOrUpdateVisible.value = true;
    saveOrUpdateTitle.value = '添加';
    coursetype.value = {
        id: 0,
        name: ''
    }
}

//  保存或更新
const saveOrUpdate = () => {
    if (saveOrUpdateTitle.value == '添加') {
        console.log("添加")
        // 调用后端接口添加
        // 
        axios.post('/coursetypeRMB', coursetype.value)
            .then(resp => {
                // console.log(resp.data)
                let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
                if (result.code == 1) {
                    // 重新获取列表
                    getArticleList();
                } else {
                    console.log('添加文章失败')
                }
            })
    } else {
        // 调用后端接口修改
        axios.put('/coursetype', coursetype.value)
            .then(resp => {
                // console.log(resp.data)
                let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
                if (result.code == 1) {
                    // 重新获取列表
                    getcoursetypeList();
                } else {
                    console.log('修改文章失败')
                }
            })
    }
    // 关闭弹框
    saveOrUpdateVisible.value = false;
    coursetype.value = {
        id: 0,
        name: ''
    }
}


//  去修改的页面
const toEdit = row => {
    saveOrUpdateVisible.value = true;
    saveOrUpdateTitle.value = '编辑';
    coursetype.value = row;

    console.log('article===', coursetype.value)
}

const getcoursetypeList = () => {
    //使用axios请求数据
    axios.get('/coursetypeRMB/getAll', {
        params: quertInfo.value
    })
        .then(res => {
            // console.log('res', res);
            let result = res.data;
            console.log('result', result.data);
            if (result.code == 1) {
                tableData.value = result.data.list;
                total.value = result.data.total;
            } else {
                console.log('获取课程列表失败')
            }
            // console.log('res',res);
            // // tableData.value = res.data.data.list;
            // console.log('tableData',tableData.value);
        })
        .catch(err => {
            console.log('err', err);
        })
}

//方法开始
//方法结束

//钩子函数开始

onMounted(() => {
    console.log('onMounted');
    getcoursetypeList();
})
//钩子函数结束




const formInline = reactive({
    name: '',
    state: '',
    date: '',
})

// 删除
// const onDel = row => {
//     ElMessageBox.confirm(
//         '真的要删除吗？',
//         '提示',
//         {
//             confirmButtonText: '确定',
//             cancelButtonText: '取消',
//             type: 'warning',
//         }
//     )
//         .then(() => {
//             ElMessage({
//                 message: '删除成功！',
//                 type: 'success',
//             })
//         })
//         .catch(() => { })
// }
// 弹框
const showPop = ref(false);
// 默认添加
const isEdit = ref(false);
const dicid = ref(0);
// 添加
const onAdd = row => {
    // 展示弹框
    showPop.value = true;
    isEdit.value = false;
}
// 编辑
const onEdit = row => {
    // console.log('row',row,row.id);
    // 展示弹框
    showPop.value = true;
    isEdit.value = true;
    // 更新id
    dicid.value = row.id;
}
// 弹框关闭
const onCancel = () => {
    // 展示弹框
    showPop.value = false;
}



const onSubmit = () => {
    console.log('submit!')
}
const tableData = ref([
    {
        id: 1,
        name: '颜色',
        state: 1,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
    {
        id: 2,
        name: '性别',
        state: 0,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
    {
        id: 3,
        name: '车牌',
        state: 1,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
])
</script>
<style scoped>
.pd20{padding: 20px;}
</style>